﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInformation;

namespace WebsiteAdmin.Controllers
{
    [Authorize]
    public class RootCataController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllRootCata(StoreRequestParameters parms)
        {
            WI_RootCataDAL rdal = new WI_RootCataDAL();
            int ttlCount;
            List<WI_RootCata> dbrootlist = rdal.GetAllRootCata(parms.Start, parms.Limit, out ttlCount);
            List<RootCata> rootlist = new List<RootCata>();
            foreach (var dbroot in dbrootlist)
            {
                RootCata rcata = RootCata.AsRootCata(dbroot);
                rootlist.Add(rcata);
            }
            return this.Store(rootlist, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteRootCata(List<RootCata> delRoots)
        {
            try
            {
                WI_RootCataDAL rdal = new WI_RootCataDAL();
                foreach (var root in delRoots)
                {
                    rdal.RemoveRootCata(root.RootCataID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewRootCataWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建栏目",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateRootCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateRootCata()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateRootCata(RootCata rootcata)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_RootCataDAL rdal = new WI_RootCataDAL();
                rdal.CreateRootCata(rootcata.RootCataName, rootcata.RootCataType, rootcata.SortNum);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "栏目创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditRootCataWindow(int rootCataId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑栏目",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditRootCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("rootCataId", rootCataId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditRootCata(int rootCataId)
        {
            WI_RootCataDAL rdal = new WI_RootCataDAL();
            WI_RootCata wircata = rdal.GetRootCata(rootCataId);
            RootCata rootcata = RootCata.AsRootCata(wircata);
            return View(rootcata);
        }

        [HttpPost]
        public ActionResult EditRootCata(RootCata rootcata)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_RootCataDAL rdal = new WI_RootCataDAL();
                rdal.UpdateRootCata(rootcata.RootCataID, rootcata.RootCataName, rootcata.RootCataType, rootcata.RootStatus, rootcata.SortNum);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "栏目更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
